package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.ejia.pojo.Admin;
import com.ejia.server.AdminService;

import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;

public class YGXXGL extends JFrame {

	private JPanel contentPane;
	private static JTable table;
	private JTextField txtname;
	private JTextField txtzw;
	private JTextField txtmm;
	private static ArrayList<Admin> admins;
	private JTextField cx;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					YGXXGL frame = new YGXXGL();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public static void dataBind(String name) {
		if (name.equals("")) {
			admins=AdminService.find("");
		}else {
			admins=AdminService.find(name);
		}
		
		Object[][] data =new Object[admins.size()][];
		for (int i = 0; i < data.length; i++) {
			data[i]= new Object[] {
			admins.get(i).getId(),
			admins.get(i).getAdminname(),
			admins.get(i).getAdminpwd(),
			admins.get(i).getRemark()
			};
		}
		table.setModel(new DefaultTableModel(
				data,
				new String[] {
					"\u7F16\u53F7", "\u7528\u6237\u540D", "\u5BC6\u7801", "\u804C\u52A1"
				}
			));
		table.setFont(new Font("宋体", 0, 26));
		table.setRowHeight(40);
		table.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 26));
	}
	
	
	/**
	 * Create the frame.
	 */
	public YGXXGL() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(YGXXGL.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setTitle("用户信息管理");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1415, 613);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(17, 126, 930, 421);
		contentPane.add(scrollPane);
		
		table = new JTable();
		dataBind("");
		scrollPane.setViewportView(table);
		
		JPanel panel = new JPanel();
		panel.setBounds(962, 126, 418, 421);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel label = new JLabel("员工账号");
		label.setFont(new Font("宋体", Font.PLAIN, 32));
		label.setBounds(15, 69, 140, 48);
		panel.add(label);
		
		JLabel label_1 = new JLabel("职务");
		label_1.setFont(new Font("宋体", Font.PLAIN, 32));
		label_1.setBounds(15, 186, 140, 48);
		panel.add(label_1);
		
		JLabel label_3 = new JLabel("密码");
		label_3.setFont(new Font("宋体", Font.PLAIN, 32));
		label_3.setBounds(15, 303, 140, 48);
		panel.add(label_3);
		
		txtname = new JTextField();
		txtname.setFont(new Font("宋体", Font.PLAIN, 26));
		txtname.setBounds(170, 73, 233, 43);
		panel.add(txtname);
		txtname.setColumns(10);
		
		txtzw = new JTextField();
		txtzw.setFont(new Font("宋体", Font.PLAIN, 26));
		txtzw.setColumns(10);
		txtzw.setBounds(170, 189, 233, 43);
		panel.add(txtzw);
		
		txtmm = new JTextField();
		txtmm.setFont(new Font("宋体", Font.PLAIN, 26));
		txtmm.setColumns(10);
		txtmm.setBounds(170, 305, 233, 43);
		panel.add(txtmm);
		
		String name=txtname.getText();
		String pwd=txtmm.getText();
		String zw=txtzw.getText();
		
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String name=txtname.getText();
				String pwd=txtmm.getText();
				String zw=txtzw.getText();
				if (name.equals("")||pwd.equals("")||zw.equals("")) {
					JOptionPane.showMessageDialog(null, "请填写完整数据");
				}else {
					if (AdminService.add(name,pwd,zw)) {
						dataBind("");
						JOptionPane.showMessageDialog(null, "添加成功");
					}
				}
			}
		});
		button.setIcon(new ImageIcon(YGXXGL.class.getResource("/com/ejia/photo/添加.png")));
		button.setFont(new Font("宋体", Font.PLAIN, 42));
		button.setBounds(234, 15, 200, 96);
		contentPane.add(button);
		
		JButton button_1 = new JButton("删除");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRowCount()>0) {
					int index= table.getSelectedRow();
					int id= admins.get(index).getId();
					if (AdminService.delete(id)) {
						dataBind("");
						JOptionPane.showMessageDialog(null, "删除成功");
					}else {
						JOptionPane.showMessageDialog(null, "删除失败");
					}
				}else {
					JOptionPane.showMessageDialog(null, "请先选择数据");
				}
			}
		});
		button_1.setIcon(new ImageIcon(YGXXGL.class.getResource("/com/ejia/photo/删除.png")));
		button_1.setFont(new Font("宋体", Font.PLAIN, 42));
		button_1.setBounds(451, 15, 200, 96);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("修改");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRowCount()>0) {
					String name=txtname.getText();
					String pwd=txtmm.getText();
					String zw=txtzw.getText();
					int index= table.getSelectedRow();
					int id= admins.get(index).getId();
					if (AdminService.update(id, name, pwd, zw)) {
						dataBind("");
						JOptionPane.showMessageDialog(null, "修改成功");
					}else {
						JOptionPane.showMessageDialog(null, "修改失败");
					}
				}else {
					JOptionPane.showMessageDialog(null, "请先选择数据");
				}
			}
		});
		button_2.setIcon(new ImageIcon(YGXXGL.class.getResource("/com/ejia/photo/修改密码.png")));
		button_2.setFont(new Font("宋体", Font.PLAIN, 42));
		button_2.setBounds(668, 15, 200, 96);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("查询");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (cx.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "请输入用户名");
				}else {
					dataBind(cx.getText());
				}
			}
		});
		button_3.setIcon(new ImageIcon(YGXXGL.class.getResource("/com/ejia/photo/查询.png")));
		button_3.setFont(new Font("宋体", Font.PLAIN, 42));
		button_3.setBounds(1172, 15, 200, 96);
		contentPane.add(button_3);
		
		cx = new JTextField();
		cx.setFont(new Font("宋体", Font.PLAIN, 24));
		cx.setBounds(885, 67, 270, 44);
		contentPane.add(cx);
		cx.setColumns(10);
		
		JLabel label_2 = new JLabel("用户名：");
		label_2.setFont(new Font("宋体", Font.PLAIN, 38));
		label_2.setBounds(883, 15, 264, 44);
		contentPane.add(label_2);
		
		JButton button_4 = new JButton("返回");
		button_4.setIcon(new ImageIcon(YGXXGL.class.getResource("/com/ejia/photo/返回 (1).png")));
		button_4.setFont(new Font("宋体", Font.PLAIN, 42));
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Main().setVisible(true);
				dispose();
			}
		});
		button_4.setBounds(17, 15, 200, 96);
		contentPane.add(button_4);
		setLocationRelativeTo(null);
	}
}
